﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using BoolanSlide.Language;
using Configuration;
using Windows.Foundation;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;


namespace BoolanElement.UI.Shapes
{
    class SelectShapes
    {
        private SelectPanel selectPanel;
        private BooDocument doc;

        private Canvas canvas;

        public SelectShapes(Canvas canvas, BooDocument doc)
        {
            this.canvas = canvas;
            this.doc = doc;

            selectPanel = new SelectPanel(261,473);
            selectPanel.Initialize(5, 3, 54, 54, new Windows.UI.Xaml.Thickness(0, 49, 0, 0),
                new Windows.UI.Xaml.Thickness(2, 2, 2, 2), LanguageManager.InsertShapePanel);
            //直线
            selectPanel.AddItem("images/LineNormal.png", "images/LineDown.png", btnClickLine);
            //直线+箭头
            selectPanel.AddItem("images/ArrowsNormal.png", "images/ArrowsDown.png", btnClickArrows);
            //曲线
            selectPanel.AddItem("images/ArcNormal.png", "images/ArcDown.png", btnClickArc);
            //正方形
            selectPanel.AddItem("images/RectangleNormal.png", "images/RectangleDown.png", btnClickRectangle);
            //圆角方形
            selectPanel.AddItem("images/RoundedRectangleNormal.png", "images/RoundedRectangleDown.png", btnClickRoundedRectangle);
            //圆
            selectPanel.AddItem("images/EllipseNormal.png", "images/EllipseDown.png", btnClickEllips);
            //正三角形
            selectPanel.AddItem("images/TriangleNormal.png", "images/TriangleDown.png", btnClickTriangle);
            //直角三角形
            selectPanel.AddItem("images/Right_TriangleNormal.png", "images/Right_TriangleDown.png", btnClickRight_Triangle);
            //正五边形
            selectPanel.AddItem("images/PentagonNormal.png", "images/PentagonDown.png", btnClickPentagon);
            //右箭头
            selectPanel.AddItem("images/Big_ArrowsNormal.png", "images/Big_ArrowsDown.png", btnClickBig_Arrows);
            //双向箭头
            selectPanel.AddItem("images/Two_ArrowsNormal.png", "images/Two_ArrowsDown.png", btnClickTwo_Arrows);
            //正菱形
            selectPanel.AddItem("images/DiamondNormal.png", "images/DiamondDown.png", btnClickDiamond);
           // 普通对话框
            selectPanel.AddItem("images/EllipseDialogNormal.png", "images/EllipseDialogDown.png", btnClickEllipseDialog);
            //圆角方形对话框
            selectPanel.AddItem("images/RectangleDialogNormal.png", "images/RectangleDialogDown.png", btnClickRectangleDialog);
            //五角形
            selectPanel.AddItem("images/Five_PointedNormal.png", "images/Five_PointedDown.png", btnClickFive_Pointed);

            Rect rect = Window.Current.Bounds;
            selectPanel.SetPosition(rect.Width - 357, UIMargin.TopSide);

        }

        public void HidePanel()
        {
            if(selectPanel != null)
                selectPanel.IsShow = false;
        }

        public bool IsShow
        {
            get
            {
                return selectPanel.IsShow;
            }
            set
            {
                selectPanel.IsShow = value;
            }
        }

        //直线
        private async void btnClickLine(object sender, RoutedEventArgs e)
        {
            LostFocusObserver.Invoke();
            await AddShapes("Line");
        }

        //带箭头直线
        private async void btnClickArrows(object sender, RoutedEventArgs e)
        {
            LostFocusObserver.Invoke();
            await AddShapes("Arrow");
        }

        //曲线
        private async void btnClickArc(object sender, RoutedEventArgs e)
        {
            selectPanel.IsShow = false;
            await AddShapes("Curve");
        }

        //正方形
        private async void btnClickRectangle(object sender, RoutedEventArgs e)
        {
            LostFocusObserver.Invoke();
            await AddShapes("Rectangle");
        }

        //圆角方形
        private async void btnClickRoundedRectangle(object sender, RoutedEventArgs e)
        {
            LostFocusObserver.Invoke();
            await AddShapes("RoundRect");
        }

        //圆
        private async void btnClickEllips(object sender, RoutedEventArgs e)
        {
            LostFocusObserver.Invoke();
            await AddShapes("Ellipse");
        }

        //正三角形
        private async void btnClickTriangle(object sender, RoutedEventArgs e)
        {
            LostFocusObserver.Invoke();
            await AddShapes("Triangle");
        }

        //直角三角形
        private async void btnClickRight_Triangle(object sender, RoutedEventArgs e)
        {
            LostFocusObserver.Invoke();
            await AddShapes("RightTriangle");
        }

        //正五边形
        private async void btnClickPentagon(object sender, RoutedEventArgs e)
        {
            LostFocusObserver.Invoke();
            await AddShapes("Pentagon");
        }

        //右向箭头
        private async void btnClickBig_Arrows(object sender, RoutedEventArgs e)
        {
            LostFocusObserver.Invoke();
            await AddShapes("RightArrow");
        }

        //双向箭头
        private async void btnClickTwo_Arrows(object sender, RoutedEventArgs e)
        {
            LostFocusObserver.Invoke();
            await AddShapes("TwoArrows");
        }

        //正菱形
        private async void btnClickDiamond(object sender, RoutedEventArgs e)
        {
            LostFocusObserver.Invoke();
            await AddShapes("Diamond");
        }

        private async void btnClickEllipseDialog(object sender, RoutedEventArgs e)
        {
            LostFocusObserver.Invoke();
            await AddShapes("EllipseDialog");
        }

        private async void btnClickRectangleDialog(object sender, RoutedEventArgs e)
        {
            LostFocusObserver.Invoke();
            await AddShapes("RoundRectDialog");
        }

        //五角星
        private async void btnClickFive_Pointed(object sender, RoutedEventArgs e)
        {
            LostFocusObserver.Invoke();
            await AddShapes("FivePointed");
        }

        private async Task<bool> AddShapes(string type)
        { 
            BooUIBase shape = ShapesFacotry.CreateShapes(type);

            canvas.Children.Add(shape.BooCanvas);

            await doc.Slides[GlobalVariable.CurrentShowIndex].AddElement(shape, shape.Element);
            return true;
        }
    }
}
